package lab4TwiceTest;

import lab2.Employee;
import lab2.Intern;
import lab2.RegularStaff;
import lab2.SalaryReference;

import java.lang.reflect.InvocationTargetException;

public class FDVisitor implements EmployeeVisitor{
    @Override
    public void visit(Employee e) {
        String name = "";
        try {
            name = (String) e.getClass().getDeclaredMethod("getName",null).invoke(e);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
            throw new RuntimeException(ex);
        }
        switch (e.getClass().getSimpleName()) {
            case "RegularStaff":
                regularStaffVisit(e, name);
                break;
            case "Intern":
                internVisit(e, name);
                break;
            //...
            default:
                throw new IllegalArgumentException("Unsupported employee type: " + e.getClass().getSimpleName());
        }
    }

    private void internVisit(Employee e, String name) {
        System.out.println(name+"‘s salary is "+SalaryReference.getSalary((Intern) e));
    }

    private void regularStaffVisit(Employee e, String name) {
        System.out.println(name+"‘s salary is "+ SalaryReference.getSalary((RegularStaff) e));
    }
}
